
import '../../model/base_response.dart';
import '../http/http_agent.dart';
import '../http/request_api.dart';
import '../model/user_model.dart';
import 'base_api_service.dart';

class AuthService extends BaseApiService {
  static final AuthService _instance = AuthService._internal();

  factory AuthService() {
    return _instance;
  }

  AuthService._internal();

  /// 用户登录
  Future<BaseResponse<UserModel>> login() async {
    final deviceId = await DeviceUtils.getPersistentDeviceId();
    return await post<UserModel>(
      RequestApi.USER_LOGIN,
      data: {
        'user_key': deviceId,
        'type': 0,
      },
      showLoading: true,
      fromJsonT: (data) => UserModel.fromJson(data),
    );
  }

    /// 绑定谷歌账号
  Future<BaseResponse<UserModel>> bindGoogleAccount(String idToken) async {
    return await post<UserModel>(
      RequestApi.BIND_GOOGLE_ACCOUNT, // 需要在 RequestApi 中定义
      data: {
        'id_token': idToken,
      },
      showLoading: true,
      fromJsonT: (data) => UserModel.fromJson(data),
    );
  }
  
  /// 绑定苹果账号
  Future<BaseResponse<UserModel>> bindAppleAccount(
    String idToken,
    String userIdentifier,
    String givenName,
    String familyName,
    String email,
  ) async {
    return await post<UserModel>(
      RequestApi.BIND_APPLE_ACCOUNT, // 需要在 RequestApi 中定义
      data: {
        'token': idToken,
        'user_identifier': userIdentifier,
        'given_name': givenName,
        'family_name': familyName,
        'email': email,
      },
      showLoading: true,
      fromJsonT: (data) => UserModel.fromJson(data),
    );
  }

  /// 用户注册
  Future<BaseResponse<UserModel>> register(
      String username, String password, String code) async {
    return await post<UserModel>(
      RequestApi.USER_LOGIN,
      data: {
        'username': username,
        'password': password,
        'code': code,
      },
      showLoading: true,
      fromJsonT: (data) => UserModel.fromJson(data),
    );
  }

  /// 退出登录
  Future<BaseResponse<bool>> logout() async {
    return await post<bool>(
      RequestApi.USER_LOGOUT,
      showLoading: false,
      fromJsonT: (data) => data == true,
    );
  }

    // 删除账户
  Future<BaseResponse<bool>>  deleteAccount() async {
     return await post<bool>(
      RequestApi.DELETE_ACCOUNT,
      showLoading: false,
      fromJsonT: (data) => data == true,
    );
  }

  /// 获取用户信息
  Future<BaseResponse<UserModel>> getUserInfo() async {
    return await get<UserModel>(
      '/user/info',
      showLoading: false,
      fromJsonT: (data) => UserModel.fromJson(data),
    );
  }
}
